package com.copa.l2379;

/**
 * @author copa
 * @createDate 2023-03-09 9:27
 * @function 滑动窗口法
 */
public class Test1 {

    public int minimumRecolors(String blocks, int k) {
        int right = k - 1;
        int result = Integer.MAX_VALUE;
        int temp = 0;
        for (int i = 0; i < k; i++) {
            if (blocks.charAt(i) == 'W') {
                temp++;
            }
        }
        result = Math.min(temp, result);
        int n = blocks.length();
        int left = 0;
        temp = result;
        while (n - 1 > right && right - left + 1 == k) {
            if (blocks.charAt(++right) == 'W') {
                temp++;
            }
            if (blocks.charAt(left++) == 'W') {
                temp--;
            }
            result = Math.min(temp, result);
        }
        return result;
    }

    public static void main(String[] args) {
        String blocks = "WBBWWBBWBW";
        int k = 7;
        System.out.println(new Test1().minimumRecolors(blocks, k));
    }
}
